﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using Ext.Net;

namespace BitPortal.Admin.Article.Article.Module
{
    public partial class ArticleAddEdit : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void BindData(long articleID = 0)
        {
            if (articleID > 0)
            {
                BitPortal.Models.Article article = ArticleBLL.GetByID(articleID);
                if (article != null)
                {
                    txtID.Text = article.ArticleID.ToString();
                    txtTitle.Text = article.Title;

                    txtDescription.RawText = txtDescription.Text = article.Description;
                    txtContent.Text = article.Content;
                    txtSortOrder.Text = article.SortOrder.ToString();
                    txtTags.Text = article.Tags;
                    cbCategory.ComboboxCategory.SelectedItem.Value = article.CategoryID.ToString();
                    datetimeDisplay.SelectedDate = (article.DatetimeDisplay == null) ? DateTime.Now : (DateTime)article.DatetimeDisplay;

                    ckIsApproved.Checked = article.IsApproved;
                    ckIsEnabled.Checked = article.IsEnabled;
                    ckIsHot.Checked = article.IsHot;
                    ckAllowComment.Checked = article.AllowComment;
                    ckHaveAttachFile.Checked = article.HaveAttachFile;
                    ckHavePhoto.Checked = article.HavePhoto;
                    ckHaveVideo.Checked = article.HaveVideo;

                    txtMetaTitle.Text = article.Meta.MetaTitle;
                    txtMetaKeyword.Text = article.Meta.MetaKeyword;
                    txtMetaDescription.Text = article.Meta.MetaDescription;
                    thumImage.TextFieldURL.Text = article.ThumbImage;

                    txtStatisticID.Text = article.StatisticID.ToString();
                    txtDiscussionGroupID.Text = article.DiscussionGroupID.ToString();

                    //Panel1.Selectable = true;
                    btnInsert.Hidden = true;
                    btnUpdate.Hidden = false;
                    Window1.Show();
                }
            }
            else
            {
                txtID.Text = "0";
                txtTitle.Text = txtDescription.Text = txtContent.Text = txtTags.Text = txtMetaTitle.Text = txtMetaDescription.Text = txtMetaKeyword.Text = "";
                ckIsApproved.Checked = ckIsEnabled.Checked = ckAllowComment.Checked = true;
                ckIsHot.Checked = ckHaveAttachFile.Checked = ckHavePhoto.Checked = ckHaveVideo.Checked = false;
                datetimeDisplay.SelectedDate = DateTime.Now;
                if (cbCategory.ComboboxCategory.Items.Count > 0)
                {
                    cbCategory.ComboboxCategory.SelectedIndex = 0;
                }
                //Panel1.Selectable = false;
                btnInsert.Hidden = false;
                btnUpdate.Hidden = true;
                Window1.Show();
            }
        }

        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleInsert");
                BitPortal.Models.Article article = new Models.Article()
                {
                    Title = txtTitle.Text.Trim(),
                    Description = txtDescription.Text,
                    Content = txtContent.Text,
                    SortOrder = Convert.ToInt64(txtSortOrder.Text),
                    Tags = txtTags.Text,
                    CategoryID = Convert.ToInt32(cbCategory.ComboboxCategory.SelectedItem.Value),
                    HaveVideo = ckHaveVideo.Checked,
                    HavePhoto = ckHavePhoto.Checked,
                    HaveAttachFile = ckHaveAttachFile.Checked,
                    Meta = new Models.Meta() { MetaTitle = txtMetaTitle.Text, MetaKeyword = txtMetaKeyword.Text, MetaDescription = txtMetaDescription.Text },
                    GroupName = txtGroup.Text,
                    IsEnabled = ckIsEnabled.Checked,
                    IsHot = ckIsHot.Checked,
                    IsApproved = ckIsApproved.Checked,
                    AllowComment = ckAllowComment.Checked,
                    DatetimeDisplay = datetimeDisplay.SelectedDate,
                    Logs = new Models.Logs() { DatetimeCreated = DateTime.Now, DatetimeEdited = DateTime.Now },
                    LanguageCode = "vi-VN",
                    ThumbImage = thumImage.TextFieldURL.Text
                };

                if (article.Insert() > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm bài viết thành công");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleEdit");
                long ID = Convert.ToInt64(txtID.Text);
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                if (article != null)
                {
                    article.Title = txtTitle.Text.Trim();

                    article.Description = txtDescription.Text;
                    article.Content = txtContent.Text;
                    article.SortOrder = Convert.ToInt64(txtSortOrder.Text);
                    article.Tags = txtTags.Text;
                    article.CategoryID = Convert.ToInt32(cbCategory.ComboboxCategory.SelectedItem.Value);
                    article.DatetimeDisplay = datetimeDisplay.SelectedDate;

                    article.IsApproved = ckIsApproved.Checked;
                    article.IsEnabled = ckIsEnabled.Checked;
                    article.IsHot = ckIsHot.Checked;
                    article.AllowComment = ckAllowComment.Checked;
                    article.HaveAttachFile = ckHaveAttachFile.Checked;
                    article.HavePhoto = ckHavePhoto.Checked;
                    article.HaveVideo = ckHaveVideo.Checked;

                    article.Meta.MetaTitle = txtMetaTitle.Text;
                    article.Meta.MetaKeyword = txtMetaKeyword.Text;
                    article.Meta.MetaDescription = txtDescription.Text;
                    article.ThumbImage = thumImage.TextFieldURL.Text;
                    if (bitEnt.SaveChanges() > 0)
                    {
                        (this.Page as Controls.BaseAdminPage).ShowNotification("Cập nhật bài viết thành công!");
                        Window1.Hide();
                        ReloadTargetData();
                    }
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }

        }

        protected void btnStatistic_Click(object sender, DirectEventArgs e)
        {
            wSatistic.AutoLoad.Url = "~/Admin/Statistic/Default.aspx?StatisticID=" + txtStatisticID.Text;
            wSatistic.LoadContent();
            wSatistic.Show();
        }

        protected void btnDiscussion_Click(object sender, DirectEventArgs e)
        {
            wDiscussion.AutoLoad.Url = "~/Admin/Comment/Default.aspx?DiscussionGroupID=" + txtDiscussionGroupID.Text;
            wDiscussion.LoadContent();
            wDiscussion.Show();
        }

    }
}